package com.example.demo.controller.back;

import com.example.demo.dao.AuthorityMapper;
import com.example.demo.dao.RoleMapper;
import com.example.demo.pojo.Role;
import com.example.demo.pojo.RoleDetails;
import com.example.demo.util.JSONResult;
import com.example.demo.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <h3>reqister</h3>
 * <p>角色管理</p>
 *
 * @author : chenjunjie
 * @date : 2021-06-24 09:01
 **/
@RestController
@RequestMapping("/back")
public class BackRoleManage {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    AuthorityMapper authorityMapper;

    @RequestMapping("/getRoleList")
    public String findRoleList(){
        List<Role> roleList = roleMapper.findRoleList();
        return JSONResult.fillResultString(HttpStatus.OK.value(),"查询成功",roleList);
    }

    @RequestMapping("/deleteRole")
    public String deleteRole(@RequestParam("roleId") Integer roleId){
        boolean success = roleMapper.deleteRole(roleId);

        return success?JSONResult.fillResultString(HttpStatus.OK.value(),"删除成功",null):
                JSONResult.fillResultString(HttpStatus.BAD_REQUEST.value(),"删除失败",null);

    }
    @RequestMapping("findRoleDetails")
    public List<RoleDetails> findRoleDetails(@RequestParam("roleId")Integer roleId){
        List<RoleDetails> parentAuthority = authorityMapper.findParentAuthority(1);
        RoleDetails roleDetails = parentAuthority.get(0);
        roleDetails.setChildren(authorityMapper.findChildrenAuthority(roleDetails.getId(),1));
        return  parentAuthority;
    }

    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    public String updateRole(RoleVo roleVo){
        boolean success =false;
        if("".equals(roleVo.getRoleId())){
            success = roleMapper.updateRole(roleVo);
        }else {


        }

        return success?JSONResult.fillResultString(HttpStatus.OK.value(),"修改成功",null):
                JSONResult.fillResultString(HttpStatus.BAD_REQUEST.value(),"修改失败",null);
    }
}
